package student.aistar.controller;

import com.github.pagehelper.PageInfo;
import com.mysql.cj.xdevapi.Session;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import student.aistar.model.bo.Result;
import student.aistar.model.entity.*;
import student.aistar.service.IIndexService;
import student.aistar.service.IOrderService;
import student.aistar.service.impl.ProductService;

import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/9/24 20:08
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @Autowired
    private ProductService productService;
    @Autowired
    private IIndexService iIndexService;

    @GetMapping("/order_load")
    public String orderload(Model model) {
        List<Navclass> navClassList = iIndexService.navClassList();
        model.addAttribute("navClassList", navClassList);
        return "mi/order/order_load";
    }

    @PostMapping("/order_list")
    public String orderList(Model model, HttpSession session, Long orderNumber, Integer orderStatus, Integer userId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize) {

        List<Navclass> navClassList = iIndexService.navClassList();
        model.addAttribute("navClassList", navClassList);

        List<OrderStatus> orderStatusList = orderService.getAllOrderStatus();

        User user = (User) session.getAttribute("user");
//        List<Orders> ordersList = orderService.getOrderByUser(user.getUserId());
        PageInfo<Orders> ordersPageInfo = orderService.getOrderByNameStatus(orderNumber, orderStatus, user.getUserId(), pageNum, pageSize);
        ArrayList<ArrayList<Orderdetail>> lists = new ArrayList<ArrayList<Orderdetail>>();
        List<Orders> ordersList = ordersPageInfo.getList();
        for (int i = 0; i < ordersList.size(); i++) {
            List<Orderdetail> orderdetailList = orderService.getOrderDetail(ordersList.get(i).getOrderId());
            lists.add((ArrayList<Orderdetail>) orderdetailList);
        }
        model.addAttribute("ordersPageInfo", ordersPageInfo);
        model.addAttribute("orderStatusList", orderStatusList);
        model.addAttribute("ordersList", ordersList);
        model.addAttribute("orderdetailList", lists);
        model.addAttribute("chooseStatus", orderStatus);
        model.addAttribute("ordernum", orderNumber);
        return "mi/order/order_list";
    }

    @GetMapping("/order_detail")
    public String orderDetail(HttpServletRequest request, Integer orderId) {
        List<Navclass> navClassList = iIndexService.navClassList();
        request.setAttribute("navClassList", navClassList);
        Orders orders = orderService.getOrdersByOrderId(orderId);
        Address address = orderService.getAddressByAddressId(orders.getAddressId());
        List<Orderdetail> orderdetailList = orderService.getOrderDetail(orderId);

        request.setAttribute("orders", orders);
        request.setAttribute("orderdetailList", orderdetailList);
        request.setAttribute("address", address);
        return "mi/order/order_detail";
    }

    @PostMapping("/del")
    @ResponseBody
    public Result del(Integer orderId) {
        int m = orderService.delOrderlById(orderId);
        if (m != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("500", "删除失败");
    }
}
